<?php
if (eregi("lang.php",$_SERVER['SCRIPT_NAME'])) {
    Header("Location: ../index.php");
    die();
}

class Lang {
// VARS:
// $available = array
// $default   = string
// $current   = string
//
// METHODS:
// init ()
// changeLang (string)


    // Language Settings
	var $available = array("ru","de"); // available languages that can be chosen
	var $default   = "ru"; // the default language
    var $current   = "ru"; // the current language


    // {{{ init()
    //
    // initialize and load the file of the current language.
    //
    // IN: -
    // OUT: -
    //
    function init() {
	    global $db, $table, $session;
		// set the current language
		if (in_array($session->language, $this->available) && file_exists("language/".$session->language.".inc.php"))
		{ // the chosen language is available and cn be loaded
		    $this->current = $session->language;
		}
		else
		{ // the chosen language is not available, we set the default language
		    $db->query("UPDATE $table->online SET language='$this->default' WHERE sessid='$session->id'");
		    $this->current = $this->default;
		}
		require_once ("language/".$this->current.".inc.php"); // load the language file
    }
	// }}}


    // {{{ changeLang()
    //
    // change the current language and refresh the site.
    //
    // IN: string [$newlang: the new langauge]
    // OUT: -
    //
    function changeLang ($newlang) {
	    global $db, $table, $session;
		// if the language file does exist
        if (file_exists("language/".$newlang.".inc.php") && in_array($newlang,$this->available)) {
		    $db->query("UPDATE $table->online SET language='$newlang' WHERE sessid='$session->id'");
            $session->language = $newlang; // change the session
			$this->current     = $newlang;
	    }
		// delete $newlang from the query
        $query = eregi_replace("newlang=[a-z]{2}", "", GLOBAL_QUERY);
		$query = eregi_replace("&{1,}", "", $query);
            if (eregi("&$", $query)) { $query = eregi_replace("&$", "", $query); }
            if ($query!="" && $query!="&") { $query = "?".$query; }
		Header ("Location: ".GLOBAL_SELF.$query); // refresh the page, so the new languge will work
    }
	// }}}


} // end class Lang
?>